﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace Tradex.Connectivity {

    /// <summary>
    /// The ConnectorOperationInfo class is sent by the different connectors to the ConnectivityService.
    /// They inform the service of a connectors exitence (should a connector be running after a connectivity
    /// service restart), but also about the "state of affairs" of the connector (queue lengths in and out,
    /// and the number of messages processed.
    /// 
    /// The ConnectorOperationInfo is not sent via normal messaging, but via the IConnectorTarget service
    /// at the well defined connectivity service URL. This is done every second.
    /// 
    /// The ConnectivityService can and should use this callbacks to collect connector performance data, but
    /// also to learn of the existence of connectors already running.
    /// </summary>
    [Serializable()]
    public sealed class ConnectionUpdate {

        /// <summary>
        /// Public constructor taking all values.
        /// </summary>
        /// <param name="inQueueLength"></param>
        /// <param name="inQueueProcessed"></param>
        /// <param name="inQueueTransfers"></param>
        /// <param name="outQueueLength"></param>
        /// <param name="outQueueProcessed"></param>
        /// <param name="outQueueTransfers"></param>
        public ConnectionUpdate(
            int inQueueLength, int inQueueProcessed, int inQueueTransfers,
            int outQueueLength, int outQueueProcessed, int outQueueTransfers
        ) {
            Timestamp = DateTime.UtcNow;
            InQueueLength = inQueueLength;
            InQueueProcessed = inQueueProcessed;
            InQueueTransfers = inQueueTransfers;
            OutQueueLength = outQueueLength;
            OutQueueProcessed = outQueueProcessed;
            OutQueueTransfers = outQueueTransfers;
        }

        /// <summary>
        /// Gets the Timstamp of the update.
        /// </summary>
        public DateTime Timestamp { get; private set; }

        /// <summary>
        /// Gets the length of the input queue.
        /// </summary>
        public int InQueueLength { get; private set; }

        /// <summary>
        /// Gets the number of items in the input queue processed since the last update.
        /// </summary>
        public int InQueueProcessed { get; private set; }

        /// <summary>
        /// Gets the number of transfers (round trips) in the input queue since the last update.
        /// </summary>
        public int InQueueTransfers { get; private set; }

        /// <summary>
        /// Gets the length of the output queue.
        /// </summary>
        public int OutQueueLength { get; private set; }

        /// <summary>
        /// Get the number of items processed in the output queue since the lat update.
        /// </summary>
        public int OutQueueProcessed { get; private set; }

        /// <summary>
        /// Gets the number of transfers from the output queue since the last update.
        /// </summary>
        public int OutQueueTransfers { get; private set; }

        /// <inheritdoc />
        public override string ToString() {
            return String.Format(
                CultureInfo.InvariantCulture, "IN: {0}/{1}/{2} OUT: {3}/{4}/{5}",
                InQueueProcessed, InQueueLength, InQueueTransfers,
                OutQueueProcessed, OutQueueLength, OutQueueTransfers
            );
        }

    }
}
